package com.hellpro.lolbox.video.view.fragment.view;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.bean.VideoSelectionBean;
import com.hellpro.lolbox.video.bean.VideoSelectionVpBean;
import com.hellpro.lolbox.video.presenter.VideoPresenter;
import com.hellpro.lolbox.video.view.VideoWebActivity;
import com.hellpro.lolbox.video.view.fragment.IVideoSelectionFragmet;
import com.youth.banner.Banner;

import java.util.List;

import butterknife.BindView;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/19
 * 备注:快看精选
 */
public class SelectionFragment extends BaseFragment implements IVideoSelectionFragmet {


    @BindView(R.id.video_selection_lv)
    PullToRefreshListView mLv;
    private LinearLayout headerView;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 3:
                    final List<VideoSelectionVpBean> vpList= (List<VideoSelectionVpBean>) msg.obj;
                    presenter.setVideoSelectionVp(vpList);
                    banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
                        @Override
                        public void OnBannerClick(View view, int position) {
                            String id = vpList.get(position).getId();
                            Log.i("TAG", "-------------------" + id);
                            String format = String.format(UrlConfigs.VIDEO_SELECTION_SECOND_URL, Integer.parseInt(vpList.get(position).getId()));
                            UIManger.startToWebActivity(getContext(),VideoWebActivity.class,format);
                        }
                    });
                    break;
                case 4:
                    final List<VideoSelectionBean> mList= (List<VideoSelectionBean>) msg.obj;
                    presenter.setVideoSelectionLvAdapter(mList,getActivity());
                    mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String format = String.format(UrlConfigs.VIDEO_SELECTION_SECOND_URL, Integer.parseInt(mList.get(position-2).getId()));
                            UIManger.startToWebActivity(getActivity(),VideoWebActivity.class,format);
                        }
                    });

                    break;
            }
        }
    };
    private VideoPresenter presenter;
    private Banner banner;

    @Override
    public void initView() {
        headerView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.video_selection_header_layout,null);
        presenter = new VideoPresenter(this,mHandler);
        presenter.setVideoSelectionVpInfo();
        presenter.setVideoSelectionInfo();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_selection;
    }

    @Override
    public Banner getBanner() {
        banner = (Banner) headerView.findViewById(R.id.video_selection_header_banner);
        return banner;
    }

    @Override
    public PullToRefreshListView getRefreshListView() {
        return mLv;
    }

    @Override
    public String getUrl() {
        return UrlConfigs.VIDEO_SELECTION_PATH;
    }

    @Override
    public LinearLayout getLvHeaderView() {
        return headerView;
    }

    @Override
    public String getVpUrl() {
        return UrlConfigs.VIDEO_SELECTION_VP_PATH;
    }

    @Override
    public Context getSelectionContext() {
        return getActivity();
    }

}
